<template>
  <div class="app-container">
    <span style="position: relative; float: right; border:1px">
      <el-button type="primary" icon="el-icon-plus" @click="addDialog" > 添加jenkins配置 </el-button>
    </span>
    <JenkinsModal ref="addConfig" :title="addTitle" operate="add"  @fetchData="fetchData"></JenkinsModal>
    <br />
    <br />
    <br />
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      resizable
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="60" prop="id">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="Jenkins名称" width="200" prop="jenkinsName">
        <template slot-scope="scope">
          {{ scope.row.jenkinsName }}
        </template>
      </el-table-column>
      <el-table-column label="服务器地址" width="300" align="center" prop="jenkinsUrl">
        <template slot-scope="scope">
          <span>{{ scope.row.jenkinsUrl }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" width="300" align="center" prop="userName">
        <template slot-scope="scope">
          {{ scope.row.userName }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="token" width="300" align="center" prop="token">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.token }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="password" label="密码">
        <template slot-scope="scope">
          <span>{{ scope.row.password}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-row :gutter="5">
            <el-col :span="40">
              <span>
                <el-tooltip content="编辑" placement="top" :hide-after="800">
                  <el-button
                    size="mini" class="operate-button"
                    @click="editDialog(scope.$index, scope.row)">
                    <i class="el-icon-edit el-icon--right" style="color: #409EFF;"></i>
                  </el-button>
                </el-tooltip>
                <JenkinsModal ref="editConfig" :title="editTitle" operate="edit" @fetchData="fetchData"></JenkinsModal>
            </span>
            </el-col>
            <el-col :span="40">
              <span>
                <el-tooltip content="删除" placement="top" :hide-after="800">
                  <el-button
                    size="mini" class="operate-button"
                    @click="delDialog(scope.$index, scope.row)">
                    <i class="el-icon-delete el-icon--right" style="color: #409EFF;"></i>
                  </el-button>
                </el-tooltip>

              </span>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getJenkinsList, delConfig } from '@/api/jenkins'
import JenkinsModal from "@/components/devops/jenkins/JenkinsModal";

export default {
  components: {JenkinsModal},
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      addTitle: "添加jenkins服务器",
      editTitle: "编辑jenkins服务器",
      list: null,
      listLoading: true,

    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getJenkinsList().then(res => {
        this.list = res.data
        this.listLoading = false
      })
    },
    addDialog() {
      this.$refs.addConfig.showDialog()
    },
    editDialog(index, row) {
      console.log('index, row: ', index, row)
      this.$refs.editConfig.showDialog(index, row)
    },
    delDialog(index, row) {
      let jenkins_name = row.jenkinsName
      let params = {
        id: row.id
      }
      console.log('index, row: ', index, row)
      this.$confirm('当前正在删除：'+ jenkins_name+' 的服务器，地址：' + row.jenkinsUrl + '，如有脚本正在使用，请先解除关联', '确认删除吗', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        delConfig(params).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.fetchData()
          } else {
            this.$message({
              type: 'error',
              message: '删除异常!'
            });
          }
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '接口调用异常!'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style lang="less" scoped>
.operate-button {
  width: 30px;
  height: 30px;
  padding: 0;
  /deep/ i {
    font-size: 16px;
  }
}
</style>
